<?php

/**
** 通用分页类。只需提供数据总数与每页显示数。
**/
class pager{
	var $url;
	var $countall;
	var $page;
	var $thestr;
	var $backstr;
	var $nextstr;
	var $pg;
	function fenye($countall,$countlist){
		@$this->pg=sprintf("%d",$_GET["page"]);
		if ($this->pg==0){
			$this->pg=1;
		}
		if (!isset($this->pg)){
			$this->pg=1;
		}
		$this->countall = $countall;
		if ($this->countall%$countlist!=0){
			$this->page=sprintf("%d",$this->countall/$countlist)+1;
		}else{
			$this->page=$this->countall/$countlist;
		}
		$this->url = self::getUrl();
		if ($this->page<=10){
			for ($i=1;$i<$this->page+1;$i++){
				$this->thestr=$this->thestr.self::makepg($i,$this->pg);
			}
		}else{
			if ($this->pg<=5){
				for ($i=1;$i<10;$i++){
					$this->thestr=$this->thestr.self::makepg($i,$this->pg);
				}
			}else{
				if (6+$this->pg<=$this->page){
					for ($i=$this->pg-4;$i<$this->pg+6;$i++){
						$this->thestr=$this->thestr.self::makepg($i,$this->pg);
					}
				}else{
					for ($i=$this->pg-4;$i<$this->page+1;$i++){
						$this->thestr=$this->thestr.self::makepg($i,$this->pg);
					}
				}
			}
		}
		$this->backstr = self::gotoback($this->pg);
		$this->nextstr = self::gotonext($this->pg,$this->page);
		//echo ($this->backstr.$this->thestr.$this->nextstr." 共".$this->countall." 条,每页".$countlist."条，分".$this->page."页");
		//echo ($this->backstr.$this->thestr.$this->nextstr);
		echo ($this->backstr.$this->nextstr.'<span class="f_l l_m5 point">当前第'.$this->pg.'页</span><div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div><span class="f_l l_m5 point">共'.$this->page.'页</span>');
	}
	function makepg($i,$pg){
		if ($i==$pg){
			return '<li data-page="'.$i.'" class="page active"><a href="javascript:void(0);">'.$i.'</a></li>';
		}else{
			return '<li data-page="'.$i.'" class="page"><a href="'.self::replacepg($this->url,5,$i).'">'.$i.'</a></li>';
		}
	}
	function gotoback($pg){
		/* if ($pg-1>0){
			return $this->gotoback='<li data-page="1" class="first"><a href="'.self::replacepg($this->url,3,0).'">首页</a></li>
	          						<li data-page="'.($this->pg-1).'" class="prev"><a href="'.self::replacepg($this->url,2,0).'">上一页</a></li>';
		}else{
			return $this->gotoback='<li data-page="1" class="first disabled"><a href="javascript:void(0);">首页</a></li>
	          						<li data-page="1" class="prev disabled"><a href="javascript:void(0);">上一页</a></li>';
		} */
		if ($pg-1>0){
			return $this->gotoback='<span class="f_l point"><a href="'.self::replacepg($this->url,3,0).'">首页</a></span>
									<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>
	          						<span class="f_l l_m5 point"><a href="'.self::replacepg($this->url,2,0).'">上一页</a></span>
	          						<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>';
		}else{
			return $this->gotoback='<span class="f_l point">首页</span>
									<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>
	          						<span class="f_l l_m5 point">上一页</span>
									<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>';
		}
	}
	function gotonext($pg,$page){
		/* if ($pg < $page){
			return '<li data-page="'.($this->pg+1).'" class="next"><a href="'.self::replacepg($this->url,1,0).'">下一页</a></li>
	          		<li data-page="'.$this->page.'" class="last"><a href="'.self::replacepg($this->url,4,0).'">末页</a></li>';
		}else{
			return '<li data-page="'.$this->page.'" class="next disabled"><a href="javascript:void(0);">下一页</a></li>
	          		<li data-page="'.$this->page.'" class="last disabled"><a href="javascript:void(0);">末页</a></li>';
		} */
		if ($pg < $page){
			return '<span class="f_l l_m5 point"><a href="'.self::replacepg($this->url,1,0).'">下一页</a></span>
					<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>
	          		<span class="f_l l_m5 point"><a href="'.self::replacepg($this->url,4,0).'">末页</a></span>
	          		<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>';
		}else{
			return '<span class="f_l l_m5 point">下一页</span>
					<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>
	          		<span class="f_l l_m5 point">末页</span>
	          		<div class="f_l w_1 h_14 bc_666 l_m5 t_m3"></div>';
		}
	}
	function replacepg($url,$flag,$i){
		if ($flag == 1){//下一页 
			$temp_pg = $this->pg;
			return str_replace("page=".$temp_pg,"page=".($this->pg+1),$url);
		}else if($flag == 2) {//上一页
			$temp_pg = $this->pg;
			return str_replace("page=".$temp_pg,"page=".($this->pg-1),$url);
		}else if($flag == 3) {//首页
			$temp_pg = $this->pg;
			return str_replace("page=".$temp_pg,"page=1",$url);
		}else if($flag == 4){//尾页
			$temp_pg = $this->pg;
			return str_replace("page=".$temp_pg,"page=".$this->page,$url);
		}else if($flag == 5){//当前页
			$temp_pg = $this->pg;
			return str_replace("page=".$temp_pg,"page=".$i,$url);
		}else{
			return $url;
		}
	}
	function getUrl(){
		$url="http://".$_SERVER["HTTP_HOST"];     
		if(isset($_SERVER["REQUEST_URI"])){
			$url.=$_SERVER["REQUEST_URI"];
		}else{
			$url.=$_SERVER["PHP_SELF"];
			if(!empty($_SERVER["QUERY_STRING"])){
				$url.="?".$_SERVER["QUERY_STRING"];
			}
		}
		if (!preg_match("/page=/i", $url)){
			if (!strpos($url,"?")){
				$url = $url."?page=1";
			}else{
				$url = $url."&page=1";
			}
		}
		return $url;
	}
}
?>